package cn.edu.neosoft.sunbo509.finefood.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import java.util.List;

import cn.edu.neosoft.sunbo509.finefood.Adapter.CommentAdapter;
import cn.edu.neosoft.sunbo509.finefood.Adapter.MyDividerItemDecoration;
import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.CollectdishesBean;
import cn.edu.neosoft.sunbo509.finefood.Bean.CommentBean;
import cn.edu.neosoft.sunbo509.finefood.Bean.DishesdetailBean;
import cn.edu.neosoft.sunbo509.finefood.Bean.IscollectBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DishesdetailActivity extends AppCompatActivity implements View.OnClickListener{
     private TextView tv_foodname1,tv_intro1,tv_price1,tv_dishescollect;
     private ImageView iv_food1,iv_dishescollect;
     private Button btn_comment,btn_purchase;
     private LinearLayout ll_dishescollect;
     private Retrofit retrofit;
     private Service service;
     private  int food_id,shop_id,bigflag;
    private String  user_id,foodname,shopname;
    private double foodprice;
    private CommentAdapter adapter;
    private List<CommentBean>list;
    private RecyclerView rv_comment;
    private LinearLayoutManager comment_Layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dishesdetail);
        getDate();
        InitView();
        initRecycleView();
        getDishesdetail();
        getComment();
        isCollect();//没点击之前应该判断是否收藏
    }
    //接收从ShopdetailAdapter传递来的数据
    public void getDate(){
        Intent intent=getIntent();
        food_id=intent.getIntExtra("foodid",0);
        user_id=intent.getStringExtra("userid");
        shop_id=intent.getIntExtra("shopid",0);
        foodname=intent.getStringExtra("foodname");
        foodprice=intent.getDoubleExtra("foodprice",1.0);
        shopname=intent.getStringExtra("shopname");
    }

    public void InitView(){
        tv_foodname1=(TextView)findViewById(R.id.tv_foodname1);
        tv_intro1=(TextView)findViewById(R.id.tv_intro1);
        tv_price1=(TextView)findViewById(R.id.tv_price1);
        iv_food1=(ImageView)findViewById(R.id.iv_food1);
        btn_comment=(Button)findViewById(R.id.btn_comment);
        btn_purchase=(Button)findViewById(R.id.btn_purchase);
        ll_dishescollect=(LinearLayout)findViewById(R.id.ll_dishescollect);
        iv_dishescollect=(ImageView)findViewById(R.id.iv_dishescollect);
        tv_dishescollect=(TextView)findViewById(R.id.tv_dishescollect);
        btn_purchase.setOnClickListener(this);
        btn_comment.setOnClickListener(this);
        ll_dishescollect.setOnClickListener(this);
    }

    private void initRecycleView() {
        rv_comment = (RecyclerView)findViewById(R.id.rv_comment);
        //创建默认的线性LayoutManager
        comment_Layout = new LinearLayoutManager(this);
        // 设置布局管理器
        rv_comment.setLayoutManager(comment_Layout);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        rv_comment.setHasFixedSize(true);
        adapter = new CommentAdapter(this);
        rv_comment.setAdapter(adapter);
        rv_comment.addItemDecoration(new MyDividerItemDecoration(this,LinearLayoutManager.VERTICAL));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_purchase:
                Intent intent=new Intent(DishesdetailActivity.this,PurchaseActivity.class);
                intent.putExtra("userid",user_id);
                intent.putExtra("foodid",food_id);
                intent.putExtra("shopid",shop_id);
                intent.putExtra("foodname",foodname);
                intent.putExtra("foodprice",foodprice);
                intent.putExtra("shopname",shopname);
                startActivity(intent);
                finish();
                break;
            case R.id. ll_dishescollect:
                if(bigflag==0) {  //flag=0，表示未收藏
                    CollectDishes();
                }
                if (bigflag==1) {  //flag=1,表示已经收藏
                    UncollectDishes();
                }
                break;
            default:
                break;
        }
    }
    //获取菜谱详情
    public void getDishesdetail(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Service.class);
        Call<DishesdetailBean>call=service.getDishes(food_id);
        call.enqueue(new Callback<DishesdetailBean>() {
            @Override
            public void onResponse(Call<DishesdetailBean> call, Response<DishesdetailBean> response) {
                Picasso.with(DishesdetailActivity.this)
                        .load(response.body().getPic())
                        .placeholder(null)
                        .into(iv_food1);
                tv_foodname1.setText(response.body().getFoodname());
                tv_intro1.setText(response.body().getIntro());
                tv_price1.setText("￥"+(int)response.body().getPrice()+"元");
            }

            @Override
            public void onFailure(Call<DishesdetailBean> call, Throwable t) {
                Log.e("error",t.getMessage());
            }
        });
    }
    //判断是否收藏菜谱 ，接口第三个参数是1
    public void isCollect(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
         service=retrofit.create(Service.class);
        Call<IscollectBean>call=service.getValue(Integer.valueOf(user_id),food_id,1);
        call.enqueue(new Callback<IscollectBean>() {
            @Override
            public void onResponse(Call<IscollectBean> call, Response<IscollectBean> response) {
                switch (response.body().getCollected()) {
                    case "1":
                        bigflag=1;//已经收藏
                        break;
                    case "0":
                        bigflag=0;//没有收藏
                        break;
                    default:
                        Toast.makeText(getApplicationContext(), "异常", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
            @Override
            public void onFailure(Call<IscollectBean> call, Throwable t) {
                Log.e("error",t.getMessage());
            }
        });
    }
    //收藏菜品
    public void CollectDishes(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Service.class);
        Call<CollectdishesBean>call=service.getdishesValue(Integer.valueOf(user_id),food_id);
        call.enqueue(new Callback<CollectdishesBean>() {
            @Override
            public void onResponse(Call<CollectdishesBean> call, Response<CollectdishesBean> response) {
                if(response.body().getSuccess().equals("1"))
                    SetCollect(1);
                else
                    SetCollect(2);
            }
            @Override
            public void onFailure(Call<CollectdishesBean> call, Throwable t) {
                Log.e("error",t.getMessage());
            }
        });
    }
    //取消收藏菜品
    public void UncollectDishes(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
         service=retrofit.create(Service.class);
        Call<CollectdishesBean>call=service.getdishesValue(Integer.valueOf(user_id),food_id);
        call.enqueue(new Callback<CollectdishesBean>() {
            @Override
            public void onResponse(Call<CollectdishesBean> call, Response<CollectdishesBean> response) {
                if(response.body().getSuccess().equals("1"))
                    SetCollect(3);
                else
                    SetCollect(4);
            }
            @Override
            public void onFailure(Call<CollectdishesBean> call, Throwable t) {
                Log.e("error",t.getMessage());
            }
        });
    }
    //获取菜品评论列表信息
    public void getComment(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
         service=retrofit.create(Service.class);
        Call<List<CommentBean>> call=service.getComment(food_id);
        call.enqueue(new Callback<List<CommentBean>>() {
            @Override
            public void onResponse(Call<List<CommentBean>> call, Response<List<CommentBean>> response) {
                list=response.body();
                adapter.setList(list);
            }
            @Override
            public void onFailure(Call<List<CommentBean>> call, Throwable t) {
                Log.e("error",t.getMessage());
            }
        });
    }

    public void SetCollect(int flag){
        switch (flag){
            case 1:
                bigflag=1;
                tv_dishescollect.setTextColor(Color.rgb(0,0,255));
                iv_dishescollect.setImageResource(R.drawable.collect1);
                Toast.makeText(getApplicationContext(),"收藏成功",Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(getApplicationContext(),"收藏失败",Toast.LENGTH_SHORT).show();
                break;
            case 3:
                tv_dishescollect.setTextColor(Color.rgb(128,128,128));
                iv_dishescollect.setImageResource(R.drawable.collect0);
                Toast.makeText(getApplicationContext(),"取消收藏成功",Toast.LENGTH_SHORT).show();
                break;
            case 4:
                Toast.makeText(getApplicationContext(),"取消收藏失败",Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getApplicationContext(),"异常",Toast.LENGTH_SHORT).show();
                break;
        }
    }

}
